/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.stepbystep;

import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.SVGImage;

/**
 * @author zinin
 */
public class GameMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command backCommand;
    private TouchSVGMenu svgMenu;
    private MIDPCanvas midpCanvas;
    private List list;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The GameMIDlet constructor.
     */
    public GameMIDlet() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        showMainMenu();
    }

    private void showMainMenu(){
        try{
            Class.forName("org.w3c.dom.events.EventListener");
            switchDisplayable(null, getSvgMenu().getSvgCanvas());
        }catch(Exception e){
            switchDisplayable(null, getList());
        }
    }
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display; if
     * <code>null</code>, then
     * <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == list) {//GEN-BEGIN:|7-commandAction|1|36-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|36-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|7-commandAction|2|36-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|98-preAction
        } else if (displayable == midpCanvas) {
            if (command == backCommand) {//GEN-END:|7-commandAction|3|98-preAction
                // write pre-action user code here
                showMainMenu();
            }//GEN-BEGIN:|7-commandAction|5|85-preAction
        } else if (displayable == svgMenu) {
            if (command == TouchSVGMenu.SELECT_COMMAND) {//GEN-END:|7-commandAction|5|85-preAction
                // write pre-action user code here
                svgMenuAction();//GEN-LINE:|7-commandAction|6|85-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
//</editor-fold>//GEN-END:|7-commandAction|8|












//<editor-fold defaultstate="collapsed" desc=" Generated Getter: midpCanvas ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of midpCanvas component.
     *
     * @return the initialized component instance
     */
    public MIDPCanvas getMidpCanvas() {
        if (midpCanvas == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            midpCanvas = new MIDPCanvas();//GEN-BEGIN:|22-getter|1|22-postInit
            midpCanvas.setTitle("midpCanvas");
            midpCanvas.addCommand(getBackCommand());
            midpCanvas.setCommandListener(this);//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return midpCanvas;
    }
//</editor-fold>//GEN-END:|22-getter|2|









//<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initialized instance of list component.
     *
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            list = new List("list", Choice.IMPLICIT);//GEN-BEGIN:|34-getter|1|34-postInit
            list.append("Start Game", null);
            list.append("High scores", null);
            list.append("Settings", null);
            list.append("About", null);
            list.append("Exit", null);
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[]{false, false, false, false, false});//GEN-END:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return list;
    }
//</editor-fold>//GEN-END:|34-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|34-action|0|34-preAction
    /**
     * Performs an action assigned to the selected list element in the list
     * component.
     */
    public void listAction() {//GEN-END:|34-action|0|34-preAction
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|34-action|1|38-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Start Game")) {//GEN-END:|34-action|1|38-preAction
                // write pre-action user code here
                switchDisplayable(null, getMidpCanvas());//GEN-LINE:|34-action|2|38-postAction
                // write post-action user code here
            } else if (__selectedString.equals("High scores")) {//GEN-LINE:|34-action|3|39-preAction
                // write pre-action user code here
//GEN-LINE:|34-action|4|39-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Settings")) {//GEN-LINE:|34-action|5|40-preAction
                // write pre-action user code here
//GEN-LINE:|34-action|6|40-postAction
                // write post-action user code here
            } else if (__selectedString.equals("About")) {//GEN-LINE:|34-action|7|41-preAction
                // write pre-action user code here
//GEN-LINE:|34-action|8|41-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Exit")) {//GEN-LINE:|34-action|9|42-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|34-action|10|42-postAction
                // write post-action user code here
            }//GEN-BEGIN:|34-action|11|34-postAction
        }//GEN-END:|34-action|11|34-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|34-action|12|
//</editor-fold>//GEN-END:|34-action|12|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgMenu ">//GEN-BEGIN:|84-getter|0|84-preInit
    /**
     * Returns an initialized instance of svgMenu component.
     *
     * @return the initialized component instance
     */
    public TouchSVGMenu getSvgMenu() {
        try{
            if (svgMenu == null) {//GEN-END:|84-getter|0|84-preInit
                // write pre-init user code here
                SVGImage svgImageMainmenu = (SVGImage) SVGImage.createImage(getClass().getResourceAsStream("/mainmenu.svg"), null);
                svgMenu = new TouchSVGMenu(svgImageMainmenu, getDisplay());//GEN-BEGIN:|84-getter|1|84-postInit
                svgMenu.setTitle("svgMenu");
                svgMenu.addCommand(TouchSVGMenu.SELECT_COMMAND);
                svgMenu.setCommandListener(this);
                svgMenu.setFullScreenMode(true);
                svgMenu.addMenuElement("button_0");
                svgMenu.addMenuElement("button_1");
                svgMenu.addMenuElement("button_2");
                svgMenu.addMenuElement("button_3");
                svgMenu.addMenuElement("button_4");//GEN-END:|84-getter|1|84-postInit
                // write post-init user code here
            }//GEN-BEGIN:|84-getter|2|
        }catch(IOException e){
        }
        return svgMenu;
    }
//</editor-fold>//GEN-END:|84-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: svgMenuAction ">//GEN-BEGIN:|84-action|0|84-preAction
    /**
     * Performs an action assigned to the selected SVG menu element in the
     * svgMenu component.
     */
    public void svgMenuAction() {//GEN-END:|84-action|0|84-preAction
        // enter pre-action user code here
        String __selectedElement = getSvgMenu().getMenuElementID(getSvgMenu().getSelectedIndex());//GEN-BEGIN:|84-action|1|87-preAction
        if (__selectedElement != null) {
            if (__selectedElement.equals("button_0")) {//GEN-END:|84-action|1|87-preAction
                // write pre-action user code here
                switchDisplayable(null, getMidpCanvas());//GEN-LINE:|84-action|2|87-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("button_1")) {//GEN-LINE:|84-action|3|88-preAction
                // write pre-action user code here
//GEN-LINE:|84-action|4|88-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("button_2")) {//GEN-LINE:|84-action|5|89-preAction
                // write pre-action user code here
//GEN-LINE:|84-action|6|89-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("button_3")) {//GEN-LINE:|84-action|7|90-preAction
                // write pre-action user code here
//GEN-LINE:|84-action|8|90-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("button_4")) {//GEN-LINE:|84-action|9|91-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|84-action|10|91-postAction
                // write post-action user code here
            }//GEN-BEGIN:|84-action|11|84-postAction
        }//GEN-END:|84-action|11|84-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|84-action|12|
//</editor-fold>//GEN-END:|84-action|12|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|97-getter|0|97-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|97-getter|0|97-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|97-getter|1|97-postInit
            // write post-init user code here
        }//GEN-BEGIN:|97-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|97-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
        
}
